# -*- coding: iso-8859-1 -*-


"""
Hexagone class - contains everything usefull to manage hexs

Catanyste - Copyright 2008-2009 Julien RAIGNEAU
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance 
with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
the specific language governing permissions and limitations under the License. 

@author J.Raigneau
"""

import wx
import hexacoord
from gamecomponents import GameComponents
from utils import opj

class HexagoneView():
    """
    Draws hexagones
    """
    
    DEFAULT = "nada"
    COLORS = {GameComponents.DESERT:[0, 0, 0],
              GameComponents.LUMBER:[36, 95, 21],
              GameComponents.GRAIN:[241, 252, 46],
              GameComponents.BRICK:[212, 51, 15],
              GameComponents.ORE:[133, 129, 128],
              GameComponents.WOOL:[94, 189, 78],
              GameComponents.WATER:[62, 67, 176],
              GameComponents.PORT3:[62, 67, 176],
              GameComponents.PORT2LUMBER:[62, 67, 176],
              GameComponents.PORT2BRICK:[62, 67, 176],
              GameComponents.PORT2WOOL:[62, 67, 176],
              GameComponents.PORT2GRAIN:[62, 67, 176],
              GameComponents.PORT2ORE:[62, 67, 176],
              DEFAULT:[255, 255, 255]}
    DESERT = GameComponents.DESERT
    SEA = GameComponents.WATER
    scale = 0
    xyshift = (0,0)

    def __init__(self, dc, uv, resource, value, hasthief ):
        """
        Inits hex class
        """
        self.uv = uv # coordonnees sur la grille
        self.resource = resource
        self.value = value
        self.hasthief = hasthief
        
        self.Draw(dc)

    def uv2xy(self, (u,v)):
        xshift, yshift = self.xyshift
        x,y = hexacoord.hexa2cart((u,v))
        return (x * self.scale + xshift, -y * self.scale + yshift)
        

    def CreateHexPath(self):
        """
        Creates hex path
        """
        vertexes = hexacoord.hex2vertexnei(self.uv) # get the vertex coordinates associated with the hex coordinates
        points = [self.uv2xy(uv) for uv in vertexes] # UV --> XY

        #useful to knom center of hex   -- shifting and scaling
        self.centerX, self.centerY = self.uv2xy(self.uv)

        return points
        
    def Draw(self, dc):
        """
        Draws hexagone
        """
        try:
            gc = wx.GraphicsContext.Create(dc)
        except NotImplementedError:
            dc.DrawText("This build of wxPython does not support the wx.GraphicsContext "
                        "family of classes.", 25, 25)
            return

        path = self.CreateHexPath()
        x0,y0 = path[0]
        xC, yC = path[3]
        r,g,b = self.COLORS[self.resource]
        oColor = wx.Colour(r,  g,  b, 80)
        cColor = wx.Colour(r,  g,  b, 255)
        gc.SetBrush(gc.CreateLinearGradientBrush(x0, y0, xC, yC, oColor, cColor))
        gc.SetPen(wx.Pen("black", 3))

        gcPath = gc.CreatePath()
        gcPath.MoveToPoint(path[0])
        for i in range(1,6):
            gcPath.AddLineToPoint(path[i])
        gcPath.AddLineToPoint(path[0])
        gc.DrawPath(gcPath)

        #TODO:make it beautiful! probably using inheritance and draw method overriding?
        # check if it is a port2 or a port3
        if self.resource == GameComponents.PORT3:
            self.__text_in_circle('3', self.COLORS[self.DEFAULT], gc, dc)

        elif self.resource == GameComponents.PORT2LUMBER:
            self.__text_in_circle('2', self.COLORS[GameComponents.LUMBER], gc, dc)

        elif self.resource == GameComponents.PORT2BRICK:
            self.__text_in_circle('2', self.COLORS[GameComponents.BRICK], gc, dc)

        elif self.resource == GameComponents.PORT2WOOL:
            self.__text_in_circle('2', self.COLORS[GameComponents.WOOL], gc, dc)

        elif self.resource == GameComponents.PORT2GRAIN:
            self.__text_in_circle('2', self.COLORS[GameComponents.GRAIN], gc, dc)

        elif self.resource == GameComponents.PORT2ORE:
            self.__text_in_circle('2', self.COLORS[GameComponents.ORE], gc, dc)
        else:
            #check if thief is on this hex
            u,v = self.uv
            if self.hasthief:
                imageFile = wx.Bitmap(opj('thief.png'))
                dc.DrawBitmap(imageFile, self.centerX-imageFile.GetWidth()/2, self.centerY-imageFile.GetHeight()/2, 1);
            elif self.resource != self.DESERT and self.resource != self.SEA and self.value != 99:
                s = "%s" % self.value
                gc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
                width, height = dc.GetTextExtent(s)
                dc.DrawText(s, self.centerX-width/2, self.centerY-height/2 )

    def __text_in_circle(self, text, color, gc, dc):
        gc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
        width, height = dc.GetTextExtent(text)
        r,g,b=color
        dc.SetPen(wx.Pen(wx.Colour(r,g,b), 1))
        dc.SetBrush(wx.Brush(wx.Colour(r,g,b)))
        radius = max(width, height)
        dc.DrawCircle(self.centerX, self.centerY, radius)
        dc.DrawText(text, self.centerX-width/2, self.centerY-height/2 )

class VertexView():
    """
    class to draw vertex, which sit at each corner of an hex
    """ 

    scale = 0
    xyshift = (0,0)

    def __init__(self, dc, uv, esperance, esperances ):
        self.uv = uv
        self.esperance = esperance
        
        self.Draw(dc)

    def Draw(self, dc):
        """
        PaintValue : paint the proba value
        """
        if self.esperance != 0:
            try:
                gc = wx.GraphicsContext.Create(dc)
            except NotImplementedError:
                dc.DrawText("This build of wxPython does not support the wx.GraphicsContext "
                            "family of classes.",25, 25)
                return

            #(X,Y) coordinate
            centerX, centerY = hexacoord.hexa2cart(self.uv)
            xshift, yshift = self.xyshift
            centerX = centerX * self.scale + xshift
            centerY = -centerY * self.scale + yshift

            # white circle
            dc.SetPen(wx.Pen('white', 1))
            dc.SetBrush(wx.Brush('white'))
            radius = 10
            dc.DrawCircle(centerX, centerY, radius)


            # text
            s = "%s" % self.esperance
            gc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
            width, height = dc.GetTextExtent(s)
            dc.DrawText(s, centerX-width/2, centerY-height/2 )
